<?php

namespace app\member\controller;

use app\member\middleware\Check;

use app\member\service\ConfigService;
use app\member\service\GroupService;
use app\member\service\RouterService;
use app\member\service\StateService;
use app\member\service\UserService;

class System
{
    protected $middleware = [
        Check::class
    ];

    /**********************************************配置*****************************************************/

    public function config(){
        return view();
    }

    public function listConfig(){
        try{
            $service = new ConfigService();
            $page = request()->get('page');
            $limit = request()->get('limit');

            $res = $service->list($page, $limit);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function addConfig(){
        try{
            $data = request()->post();
            $service = new ConfigService();
            $res = $service->add($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function editConfig(){
        try{
            $data = request()->post();
            $service = new ConfigService();
            $res = $service->edit($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function softdeleteConfig(){
        try{
            $ids = request()->post('ids');
            $service = new ConfigService();
            $res = $service->softdelete($ids);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }


    /**********************************************配置*****************************************************/

    /**********************************************路由*****************************************************/

    public function router(){
        return view();
    }

    public function treeRouter(){
        try{
            $service = new RouterService();
            $res = $service->treeList();
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function listRouter(){
        try{
            $service = new RouterService();
            $page = request()->get('page');
            $limit = request()->get('limit');
            $where = [];
            $router_name = request()->get('router_name');
            if(!isEmpty($router_name)){
                array_push($where,['routers.router_name','like','%'.$router_name.'%']);
            }

            $res = $service->list($page, $limit,$where);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function addRouter(){
        try{
            $data = request()->post();
            $service = new RouterService();
            $res = $service->add($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function editRouter(){
        try{
            $data = request()->post();
            $service = new RouterService();
            $res = $service->edit($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function softdeleteRouter(){
        try{
            $ids = request()->post('ids');
            $service = new RouterService();
            $res = $service->softdelete($ids);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }



    /**********************************************路由*****************************************************/




    /**********************************************用户*****************************************************/

    public function user(){
        return view();
    }

    public function listUser(){
        try{
            $service = new UserService();
            $page = request()->get('page');
            $limit = request()->get('limit');

            $nickname = request()->get('nickname');

            $where = [];

            if(!isEmpty($nickname)){
                array_push($where, ['nickname', 'like', '%' . $nickname . '%']);
            }

            $res = $service->list($page, $limit,$where);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function addUser(){
        try{
            $data = request()->post();
            $service = new UserService();
            $res = $service->add($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function editUser(){
        try{
            $data = request()->post();
            $service = new UserService();
            $res = $service->edit($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function softdeleteUser(){
        try{
            $ids = request()->post('ids');
            $service = new UserService();
            $res = $service->softdelete($ids);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    /**
     * Notes:加载权限下拉框
     * User: ZHY
     * DateTime: 2023/7/6 22:42
     * @return \think\response\Json
     */
    public function loadGroupSelect(){
        try{
            $service = new GroupService();
            $res = $service->listAll();
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function loadStateSelect(){
        try{
            $service = new StateService();
            $res = $service->listAll();
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }



    /**********************************************用户*****************************************************/


    /**********************************************状态*****************************************************/

    public function state(){
        return view();
    }

    public function listState(){
        try{
            $service = new StateService();
            $page = request()->get('page');
            $limit = request()->get('limit');
            $res = $service->list($page, $limit);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function addState(){
        try{
            $data = request()->post();
            $service = new StateService();
            $res = $service->add($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function editState(){
        try{
            $data = request()->post();
            $service = new StateService();
            $res = $service->edit($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function softdeleteState(){
        try{
            $ids = request()->post('ids');
            $service = new StateService();
            $res = $service->softdelete($ids);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    /**********************************************状态*****************************************************/


    /**********************************************职务*****************************************************/

    public function group()
    {
        return view();
    }

    public function getRouters(){
        try{
            $ids = request()->post("ids");
            $service = new RouterService();
            $res = $service->getBottomRouter($ids);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }


    /**
     * Notes:权限分页
     * User: ZHY
     * DateTime: 2023/7/5 21:06
     * @return \think\response\Json
     */
    public function listGroup()
    {
        try {
            $service = new GroupService();
            $page = request()->get('page');
            $limit = request()->get('limit');
            $res = $service->list($page, $limit);
            return json($res);
        } catch (\Throwable $ex) {
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function addGroup()
    {
        try {
            $data = request()->post();
            $service = new GroupService();
            $res = $service->add($data);
            return json($res);
        } catch (\Throwable $ex) {
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function softdeleteGroup(){
        try{
            $ids = request()->post('ids');
            $service = new GroupService();
            $res = $service->softdelete($ids);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操䤱败,错误信息:' . $ex->getMessage()));
        }
    }

    public function editGroup(){
        try{
            $data = request()->post();
            $service = new GroupService();
            $res = $service->edit($data);
            return json($res);
        }catch(\Throwable $ex){
            return json(AjaxReturn(-1, '操䤱败,错误信息:' . $ex->getMessage()));
        }
    }

    /**********************************************职务*****************************************************/




}